package edu.njfu.networkdemo

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import com.google.gson.Gson
import edu.njfu.networkdemo.model.ResultVO
import edu.njfu.networkdemo.utils.FileUtil
import edu.njfu.networkdemo.utils.HttpUtil
import kotlinx.android.synthetic.main.activity_main.*
import okhttp3.*
import java.io.IOException
import java.net.HttpURLConnection
import java.net.URL
import kotlin.concurrent.thread

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        submit.setOnClickListener {
           startActivity(Intent(this,SixthActivity::class.java));
            //1. 获取用户名与密码的值
            //val data="username=${et_username.text.toString()}&password=${et_password.text.toString()}"
            //Toast.makeText(this,data,Toast.LENGTH_LONG).show()
//            val intentOk: Intent = Intent(this, SecondActivity::class.java)
//            val intentError: Intent = Intent(this, ThirdAcitivity::class.java)
//            val requestData=HashMap<String,String>();
//            requestData.put("username",et_username.text.toString())
//            requestData.put("password",et_password.text.toString())
//             //sendRequestWithHttpURLConnection(data)
//           // sendRequestWithOkHttp("http://106.15.192.167:8080/userLogin","post",requestData)
//            HttpUtil.sendOkHttpRequest("http://106.15.192.167:8080/userLogin",
//                requestData,object:Callback{
//                    override fun onFailure(call: Call, e: IOException) {
//                        Log.e("MainActivity","发生错误了")
//                    }
//
//                    override fun onResponse(call: Call, response: Response) {
//                        val resultData = response.body?.string()
//                        var fromJson = Gson().fromJson<ResultVO>(resultData, ResultVO::class.java)
//                        if (fromJson.result == "ok") {
//                            startActivity(intentOk)
//                        } else {
//                            //登录失败,跳转到third
//                            startActivity(intentError)
//                        }
//                    }
//                })
        }
    }
    /**
     * android 网络编程
     * HttpURLConnection
     * val url=URL("http://106.15.192.167/userLogin")
     * val conn=url.openConnection() as HttpURLConnction
     * conn.requestMethod="POST"
     * conn.connectionTiemout=8000
     * conn.readTiemout=8000
     *
     */
//    private fun sendRequestWithHttpURLConnection(data:String):Unit{
//        //开启一个新的线程，发起网络请求
//        thread{
//            var  conn:HttpURLConnection?=null
//            try {
//                //1.获取连接
//                val url = URL("http://106.15.192.167:8080/userLogin")
//                conn = url.openConnection() as HttpURLConnection
//                //2. 设置连接参数
//                conn.requestMethod="POST"
//                conn.connectTimeout=8000
//                conn.readTimeout=8000
//                //3.发送数据，获取输出流
//                var output = conn.outputStream
//                //4. 以字节数组方式发送数据
//                output.write(data.toByteArray())
//                //5. 立即发送
//                output.flush()
//
//                //6. 获取发送状态码
//                var responseCode = conn.responseCode
//                //7. 对状态码进行判断
//                // Log.e("MainActivity","返回码为：${responseCode}")
//                if(responseCode==200){
//                    //8.接收数据
//                    var input = conn.inputStream
//                    var result = FileUtil.getStringFromInputStream(input)
//                    Log.e("MainActivity",result)
//                    var resultOV = Gson().fromJson<ResultVO>(result, ResultVO::class.java)
//                    //登录成功,
//                    if(resultOV.result=="ok"){
//                        //跳转到Sencond
//                        val intentOk: Intent= Intent(this,SecondActivity::class.java)
//                        startActivity(intentOk)
//
//                    }else{
//                        //登录失败,跳转到third
//                        val intentError:Intent=Intent(this,ThirdAcitivity::class.java)
//                        startActivity(intentError)
//                    }
//                }
//
//            }catch (e: Exception ){
//                e.printStackTrace()
//            }finally {
//
//            }
//        }
//    }
//    private fun  sendRequestWithOkHttp(url:String,method:String,data:Map<String,String>){
//        thread{
//            try{
//                val client= OkHttpClient()
//                val userName=data.get("username").toString()
//                val password=data.get("password").toString()
//                Log.e("Okhttps",userName+"----"+password)
//                val requestBody=FormBody.Builder()
//                    .add("username",userName)
//                    .add("password",password)
//                    .build()
//                val request= Request.Builder()
//                    .url(url)
//                    .post(requestBody)
//                    .build()
//                val response=client.newCall(request) .execute()
//                val responseData=response.body?.string()
//                if(responseData!=null){
//                   // dealResponse(responseData)
//                    Log.e("OKhttp",responseData)
//                }
//            }catch (e:Exception){
//                e.printStackTrace()
//            }
//        }
 //   }

//    private fun dealResponse(responseData: String) {
//        Log.d("MainActivity",responseData);
//        Toast.makeText(this,responseData,Toast.LENGTH_LONG).show()
//        var result = Gson() .fromJson(responseData,ResultVO::class.java)
//        if(result.result=="ok"){
//            //跳转到Sencond
//            val intentOk: Intent= Intent(this,SecondActivity::class.java)
//            startActivity(intentOk)
//
//        }else{
//            //登录失败,跳转到third
//            val intentError:Intent=Intent(this,ThirdAcitivity::class.java)
//            startActivity(intentError)
//        }
//    }
}
